package com.renit.gallery.adapter

import android.graphics.drawable.Drawable
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.navigation.findNavController
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.renit.gallery.PhotoItem
import com.renit.gallery.R
import com.renit.gallery.TAG

class GalleryAdapter: PagedListAdapter<PhotoItem, GalleryViewHolder>(DIFFCALLBACK){

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GalleryViewHolder {
        val holder = GalleryViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.gallery_cell, parent, false))
        holder.itemView.setOnClickListener{
            Bundle().apply {
                putParcelableArrayList("PHOTO_LIST", ArrayList(currentList!!))
                putInt("PHOTO_POS", holder.adapterPosition)
                holder.imageView.findNavController().navigate(R.id.action_homeFragment_to_photoFragment, this)
            }
        }
        return holder
    }

    override fun onBindViewHolder(holder: GalleryViewHolder, position: Int) {
        val item = getItem(position)?:return
        holder.let {
            it.imageView.layoutParams.apply {
                this.height = item.photoHeight
                this.width = item.photoWidth
            }
            it.txLikes.text = item.likes.toString()
            it.txUser.text = item.user
            it.txFa.text = item.comments.toString()
        }

        Glide.with(holder.itemView)
            .load(item.previewUrl)
            .placeholder(R.drawable.ic_baseline_photo_size_select_actual_24)
            .listener(object :RequestListener<Drawable> {
                override fun onLoadFailed(
                    e: GlideException?,
                    model: Any?,
                    target: Target<Drawable>?,
                    isFirstResource: Boolean
                ): Boolean {
                    Log.e(TAG, "onLoadFailed: ")
                    return false
                }

                override fun onResourceReady(
                    resource: Drawable?,
                    model: Any?,
                    target: Target<Drawable>?,
                    dataSource: DataSource?,
                    isFirstResource: Boolean
                ): Boolean {
                    return false
                }

            })
            .into(holder.imageView)
    }

    object DIFFCALLBACK: DiffUtil.ItemCallback<PhotoItem>() {
        override fun areItemsTheSame(oldItem: PhotoItem, newItem: PhotoItem): Boolean {
            return oldItem === newItem
        }

        override fun areContentsTheSame(oldItem: PhotoItem, newItem: PhotoItem): Boolean {
            return oldItem.photoId == newItem.photoId
        }
    }
}

class GalleryViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val imageView: ImageView = itemView.findViewById(R.id.imageView)
    val txLikes: TextView = itemView.findViewById(R.id.tx_likes)
    val txUser: TextView = itemView.findViewById(R.id.tx_user)
    val txFa: TextView = itemView.findViewById(R.id.tx_fa)
}